package web.jsoft.com;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.springframework.security.authentication.AuthenticationDetailsSource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;

@ManagedBean
@RequestScoped
public class LoginBean {

    private AuthenticationManager authenticationManager;
    //private AuthenticationDetailsSource ads = new WebAuthenticationDetailsSource();
    private String userName;
    private String password;  

    public void setAuthenticationManager(AuthenticationManager authenticationManager){
    	this.authenticationManager = authenticationManager;
    }
    
    public AuthenticationManager getAuthenticationManager(){
    	return this.authenticationManager;
    }
    
    public String login() {
        try {
        	
            Authentication request = new UsernamePasswordAuthenticationToken(this.getUserName(), getPassword());
            Authentication result = authenticationManager.authenticate(request);
            SecurityContextHolder.getContext().setAuthentication(result);/*
        	Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
        	if (authentication == null) {
        	      // User isn't authenticated. Check if there is a Google Accounts user
        	      User googleUser = UserServiceFactory.getUserService().getCurrentUser();

        	      if (googleUser != null) {
        	        // User has returned after authenticating through GAE. Need to authenticate to Spring Security.
        	        PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(googleUser, null);
        	        token.setDetails(ads.buildDetails(request));

        	        try {
        	          authentication = authenticationManager.authenticate(token);
        	          // Setup the security context
        	          SecurityContextHolder.getContext().setAuthentication(authentication);
        	          // Send new users to the registration page.
        	          if (authentication.getAuthorities().contains(AppRole.NEW_USER)) {
        	            ((HttpServletResponse) response).sendRedirect(REGISTRATION_URL);
        	              return;
        	          }
        	        } catch (AuthenticationException e) {
        	         // Authentication information was rejected by the authentication manager
        	          failureHandler.onAuthenticationFailure((HttpServletRequest)request, (HttpServletResponse)response, e);
        	          return;
        	        }
        	      }*/
        } catch (AuthenticationException e) {
            String loginFailedMessage = FacesUtils.getBundleKey("msg", "login.failed");
            FacesUtils.addErrorMessage(loginFailedMessage);
            return null;
        }
        return "/ui/welcome.jsf";
    }
   
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
